<?php
class Merchant_Model_Receiving
{
	private $_table;
	public function __construct(){
		$this->_table = new Merchant_Model_DbTable_Receiving();
	}

	/**
	*add DB Adapter
	*/
	public function getAdapter(){
		return $this->_table->getAdapter();
	}
	
	public function add($row){
		return $this->_table->insert($row);
	}

	public  function update($row,$value,$field='receiving_id'){
		$row['modified'] = date("Y-m-d H:i:s");
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?",$value);		
		return $this->_table->update($row, $where);
	}

	public function delete($value,$field='receiving_id'){
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?", $value);
		return $this->_table->delete($where);
	}

	public function getByValue($value,$field='receiving_id'){
        $select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,'*');
        $select->where("{$field} = ?",$value);
		return $this->_table->getAdapter()->fetchRow($select);
	}

	public function getByCond($cond,$type,$order='',$count=20, $offset=0){
		$table = $this->_table->info('name');
		$select = $this->_table->select()->from($table, $type);
		$select->where('customer_id = ?',$cond['customer_id']);
		if(isset($cond['status']) && $cond['status'] !==''){
			$select->where('status = ?',$cond['status']);
		}
		if(isset($cond['receiving_code']) && $cond['receiving_code'] !=''){
			$select->where('receiving_code = ?',$cond['receiving_code']);
		}
		if(isset($cond['order_type']) && $cond['order_type'] !=''){
			$select->where('order_type = ?',$cond['order_type']);
		}
		if(isset($cond['ref_id']) && $cond['ref_id'] !=''){
			$select->where('ref_id like ?','%'.$cond['ref_id'].'%');
		}
		if(isset($cond['dateFrom']) && $cond['dateFrom'] !=''){
			$select->where('created > ?',$cond['dateFrom'].' 00:00:00');
		}
		if(isset($cond['dateTo']) && $cond['dateTo'] !=''){
			$select->where('created < ?',$cond['dateTo'].' 23:59:59');
		}
		if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }
        if ($count==0){        	
        	return $this->_table->fetchAll($select)->toArray(); 
        }
		$select->order($order);
		$select->limit($count,$offset);
	
		return $this->_table->fetchAll($select)->toArray();
	}
	
	public function getRMAListByCond($cond,$type,$order='',$count=20, $offset=0){
		$table = $this->_table->info('name');
		$select = $this->_table->select()->from($table, $type);
		$select->where('customer_id = ?',$cond['customer_id']);
		$select->where(' order_type in(2,3) ');
		if(isset($cond['status']) && $cond['status'] !==''){
			$select->where('status = ?',$cond['status']);
		}
		if(isset($cond['receiving_code']) && $cond['receiving_code'] !=''){
			$select->where('receiving_code = ?',$cond['receiving_code']);
		}
		if(isset($cond['ref_id']) && $cond['ref_id'] !=''){
			$select->where('ref_id = ?',$cond['ref_id']);
		}
		if(isset($cond['dateFrom']) && $cond['dateFrom'] !=''){
			$select->where('created > ?',$cond['dateFrom'].' 00:00:00');
		}
		if(isset($cond['dateTo']) && $cond['dateTo'] !=''){
			$select->where('created < ?',$cond['dateTo'].' 23:59:59');
		}
		if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }
        if ($count==0){        	
        	return $this->_table->fetchAll($select)->toArray(); 
        }
		$select->order($order);
		$select->limit($count,$offset);

		return $this->_table->fetchAll($select)->toArray();
	}
	
	/**
	 * 获取当天最后一个 receiving_code
	 */
	public function getCurdateLastCode($customer_id,$order_type="1"){
		$select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,'max(receiving_code)');
        $mindate = date('Y-m-d').' 00:00:00';
        $maxdate = date('Y-m-d').' 23:59:59';

        $select->where("created > ?",$mindate);
        $select->where("created < ?",$maxdate);
        $select->where("customer_id = ?",$customer_id);  
        $select->where("order_type = ?",$order_type);  
        
		return $this->_table->getAdapter()->fetchOne($select);
	}
	
	/**
	 * 获取当天最后一个 退货code
	 */
	public function getCurdateLastReturnCode($customer_id){
		$select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,'max(receiving_code)');
        $mindate = date('Y-m-d').' 00:00:00';
        $maxdate = date('Y-m-d').' 23:59:59';

        $select->where("created > ?",$mindate);
        $select->where("created < ?",$maxdate);
        $select->where("order_type in (?)",array(2,3));
        $select->where("customer_id = ?",$customer_id); 
		return $this->_table->getAdapter()->fetchOne($select);
	}
	
	/**
	* 根据产品查询未发货的订单
	*
	*/
	public function getByProductId($productId){
		$select = $this->_table->getAdapter()->quoteInto("select count(*) from receiving,receiving_item where receiving.receiving_code = receiving_item.receiving_code and receiving.status in(1) and receiving_item.product_id = ?",$productId);
		return $this->_table->getAdapter()->fetchOne($select);
	}
	
}